import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout/Layout'

Vue.use(VueRouter)

export const routes = [
  {
    path: '/login',
    component: () => import('@/views/user/Login'),
    name: 'Login',
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/inventory',
    children: [
      {
        path: '/inventory',
        component: () => import('@/views/inventory/InventoryManage'),
        name: 'Inventory',
        meta: { title: '库存管理' }
      },
      {
        path: '/customer',
        component: () => import('@/views/customer/CustomerList'),
        name: 'Customer',
        meta: { title: '客户管理' }
      },
      {
        path: '/inbound',
        component: () => import('@/views/inbound/InboundOrder'),
        name: 'InboundOrder',
        meta: { title: '入库管理' }
      },
      {
        path: '/outbound',
        component: () => import('@/views/outbound/OutboundOrder'),
        name: 'OutboundOrder',
        meta: { title: '出库管理' }
      },
      {
        path: '/supplier',
        component: () => import('@/views/supplier/SupplierList'),
        name: 'Supplier',
        meta: { title: '供应商管理' }
      },
      {
        path: '/supplier-product',
        component: () => import('@/views/supplier/SupplierProduct'),
        name: 'SupplierProduct',
        meta: { title: '供应商商品' }
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const user = sessionStorage.getItem('user')
  
  if (to.path === '/login') {
    if (user) {
      next('/inventory')
    } else {
      next()
    }
  } else {
    if (!user) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router 